package com.etongwl.mylibrary;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 生产看板Tab主界面
 */
public class GenerateBillBoardActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {

    @BindView(R.id.tablayout)
    TabLayout mTabLayout;
    @BindView(R.id.app_bar)
    AppBarLayout appBar;
    @BindView(R.id.toolbar_layout)
    CollapsingToolbarLayout toolbarLayout;
    @BindView(R.id.generatebill_board_viewpager)
    ViewPager viewPager;
    @BindView(R.id.generatebill_board_layout)
    LinearLayout linearLayout;

    boolean runAnimat=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_generate_bill_board);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        List<String> titles = new ArrayList<>();
        titles.add("生产");
        titles.add("异常");
        titles.add("常量");
        titles.add("停机");
        titles.add("未排");
        for (int i = 0; i < titles.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(i)));
        }

        List<Fragment> listFragment = new ArrayList<>();
        GenerateFragmentAdapter adapter =new GenerateFragmentAdapter(getSupportFragmentManager(),listFragment);
        viewPager.setAdapter(adapter);

        appBar.addOnOffsetChangedListener(this);
        appBar.setExpanded(false,true);

        Thread thread=new Thread(){
            @Override
            public void run() {
                try {
                    int m =10;
                    for (int i=0;i<m;i++){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (runAnimat){
                                    runAnimat = false;
                                    appBar.setExpanded(true,true);
                                }else{
                                    runAnimat = true;
                                    appBar.setExpanded(false,true);
                                }

                            }
                        });
                        Thread.sleep(2000);
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        thread.start();
    }


//    int m=0;
    boolean runAnimation=false;//是否执行动画 true执行动画 false不执行动画
    int state =1;//1是上滑动 2是下滑动
    int stateCurrent =0;//1是上滑动 2是下滑动 默认

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        Log.e("aaa", "verticalOffset=" + verticalOffset);
    }

    /**
     * 生成适配器
     */
    private class GenerateFragmentAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments;

        public GenerateFragmentAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.fragments = list;
        }

        @Override
        public Fragment getItem(int position) {
            if (fragments!=null&&fragments.size()>0){
                return fragments.get(position);
            }else{
                return null;
            }
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }


}
